package lostland.gmud.exv2.js;

import org.mozilla.javascript.Scriptable;

import lostland.gmud.exv2.blgframework.CScreen;

/**
 * Created by 12548 on 2015/9/13.
 */
public class LinearScriptScreen extends CScreen {

	Scriptable scope;
	int line = 0;
	String functionName;

	public LinearScriptScreen(String script, String functionName){
		scope = JsCommon.prepareScriptToExec(script);
		this.functionName = functionName;
	}

	public LinearScriptScreen(String script){
		scope = JsCommon.prepareScriptToExec(script);
		this.functionName = "gogogo";
	}

	@Override
	public void present(float deltaTime) {

	}

	@Override
	public void pause() {

	}

	@Override
	public void resume() {

	}

	@Override
	public void dispose() {
		JsCommon.execScriptOver();
		if(JsRoom.getInstance().currentLSS == this){
			JsRoom.getInstance().currentLSS = null;
		}
	}

	@Override
	public void update(float deltaTime) {
		JsRoom.getInstance().currentLSS = this;
		String s = JsCommon.execScriptFunction(scope, functionName, new Object[]{line});
//		Logger.d("line to " + s);
		line = (int) Math.floor(Double.valueOf(s));
		if(line < 0) this.removeFromGame();
	}
}
